<?php
	require_once("Service.php");
	require_once("Database.php");
	require_once("vo/UserVO.php");
	require_once("vo/BlogPostVO.php");
	require_once("vo/BlogCommentVO.php");
	
	/**
	 * Class which handles all user profile related queries.
	 */
	class UserProfileService extends Service {
		
		var $conn;
		
		/** The constructor of this object. */
		function __construct() {
			
			parent::__construct();
			$this->conn = new Database();
		}
		
		/**
		 * Finds a user's user profile information.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $userUID The user who's profile to find.
		 * @return An object containing the user's profile data.
		 */
		function getUserProfile($sessionID, $userUID) {
			
			$this->validateSession($sessionID);
			
			$result = array();
			
			// get the user's personal details
			$query = Database::buildQuery("
				SELECT
					userUID,
					firstName,
					lastName,
					email,
					phoneNumber,
					description,
					gender,
					dateOfBirth,
					location,
					occupation,
					institution,
					status
				FROM Users
				WHERE userUID = ?
			", $userUID);
			$qryUserDetails = $this->conn->execute($query);
			
			$recUserDetails = mysql_fetch_assoc($qryUserDetails);
			$result["userData"] = UserVO::mapObject($recUserDetails);
			$result["status"] = $recUserDetails["status"];
			
			// get the user's blog posts
			$query = Database::buildQuery("
				SELECT
					BlogPosts.blogPostUID,
					BlogPosts.heading,
					BlogPosts.message,
					BlogPosts.dateCreated,
					Users.userUID,
					Users.firstName,
					Users.lastName
				FROM BlogPosts
				INNER JOIN Users
					ON Users.userUID = BlogPosts.userUID
				WHERE BlogPosts.userUID = ?
				ORDER BY BlogPosts.dateCreated ASC
			", $userUID);
			$qryUserBlogPosts = $this->conn->execute($query);
			
			// get comments made on blog posts
			$posts = array();
			while($recUserBlogPosts = mysql_fetch_assoc($qryUserBlogPosts)) {
				
				$query = Database::buildQuery("
					SELECT
						BlogComments.blogCommentUID,
						BlogComments.blogPostUID,
						BlogComments.message,
						BlogComments.dateCreated,
						Users.userUID,
						Users.firstName,
						Users.lastName
					FROM BlogComments
					INNER JOIN Users
						ON Users.userUID = BlogComments.userUID
					WHERE BlogComments.blogPostUID = ?
					ORDER BY BlogComments.dateCreated ASC
				", $recUserBlogPosts["blogPostUID"]);
				$qryBlogComments = $this->conn->execute($query);
				
				$comments = array();
				while($recBlogComments = mysql_fetch_assoc($qryBlogComments)) {
					$comments[] = BlogCommentVO::mapObject($recBlogComments);
				}
				
				$post = BlogPostVO::mapObject($recUserBlogPosts);
				$post->comments = $comments;
				$posts[] = $post;
			}
			
			$result["posts"] = $posts;
			
			return $result;
		}
		
		/**
		 * Saves a user's status message.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $userUID The user who's status to update.
		 * @param $status The new status message of the user.
		 */
		function saveUserStatus($sessionID, $userUID, $status) {
			
			$this->validateSession($sessionID);
			
			$query = Database::buildQuery("
				UPDATE Users
				SET status = ?
				WHERE userUID = ?
			", $status, $userUID);
			$this->conn->execute($query);
		}
		
		/**
		 * Saves a new user blog post.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $userUID The user who made the blog post.
		 * @param $heading The heading of the post.
		 * @param $message The message body of the post.
		 * @return The blogPostUID of the saved blog post.
		 */
		function saveBlogPost($sessionID, $userUID, $heading, $message) {
			
			$this->validateSession($sessionID);
			
			$blogPostUID = $this->conn->generateUID();
			
			$query = Database::buildQuery("
				INSERT INTO BlogPosts(
					blogPostUID,
					userUID,
					heading,
					message,
					dateCreated
				) VALUES (?, ?, ?, ?, ?)
			", $blogPostUID, $userUID, $heading, $message, date("Y-m-d H:i:s"));
			$this->conn->execute($query);
			
			return $blogPostUID;
		}
		
		/**
		 * Saves a comment on a blog post.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $blogPostUID The blog post the comment was made on.
		 * @param $userUID The user who made the comment.
		 * @param $message The message body of the comment.
		 * @return The blogCommentUID of the saved blog comment.
		 */
		function saveBlogComment($sessionID, $blogPostUID, $userUID, $message) {
			
			$this->validateSession($sessionID);
			
			$blogCommentUID = $this->conn->generateUID();
			
			$query = Database::buildQuery("
				INSERT INTO BlogComments(
					blogCommentUID,
					blogPostUID,
					userUID,
					message,
					dateCreated
				) VALUES (?, ?, ?, ?, ?)
			", $blogCommentUID, $blogPostUID, $userUID, $message, date("Y-m-d H:i:s"));
			$this->conn->execute($query);
			
			return $blogCommentUID;
		}
	}
?>